package com.yc.fresh.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.fresh.bean.GoodsInfo;
import com.yc.fresh.service.IGoodsInfoService;
import com.yc.fresh.util.StringUtil;
import com.yc.fresh.vo.ResultVO;

/**
 * 商品信息控制层
 * @author H
 * @date 2021年7月16日
 */
@RestController
@RequestMapping("/goods")
public class GoodsInfoController {
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	/**
	 * 处理图文混编中的图片上传
	 * @param pic
	 * @param request
	 * @return
	 */
	@PostMapping("/upload")
	public Map<String, Object> upload(@RequestParam("upload") MultipartFile pic, HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String savePath = "pics";
			String temp = request.getServletContext().getInitParameter("uploadPath");//获取用户配置路径
			if(!StringUtil.checkNull(temp)) {
				savePath = temp;
			}
			
			String path = request.getServletContext().getRealPath("");//获取tomcat在服务器中的绝对路径
			
			savePath += "/" + new Date().getTime() + "_" + pic.getOriginalFilename();//重命名上传的文件
			
			File dest = new File(new File(path).getParentFile(),savePath);
			
			//将本地文件保存到服务器
			pic.transferTo(dest);
			
			map.put("fileName", pic.getOriginalFilename());
			map.put("uploaded", 1);
			map.put("url", "../../../" + savePath);
		}catch(IllegalStateException e){
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@PostMapping("/add")
	public ResultVO addGood(@RequestParam("pic") MultipartFile[] pics, GoodsInfo gf, HttpServletRequest request) {
		if(pics != null && pics.length > 0 && !pics[0].isEmpty()) {
			try {
				String savePath = "pics";
				String temp = request.getServletContext().getInitParameter("uploadPath");//获取用户配置路径
				if(!StringUtil.checkNull(temp)) {
					savePath = temp;
				}
				
				String basePath = request.getServletContext().getRealPath("");//获取tomcat在服务器中的绝对路径
				String picStr = "";
				File dest = null;
				String path = null;
				
				for(MultipartFile fl : pics) {
					path = savePath + "/" + new Date().getTime() + "_" + fl.getOriginalFilename();//重命名上传的文件
					dest = new File(new File(basePath).getParentFile(),path);
					fl.transferTo(dest);
					picStr += path + ";";
				}
				
				if(!"".equals(picStr)) {
					picStr = picStr.substring(0, picStr.lastIndexOf(";"));
				}
				
				gf.setPics(picStr);
			}catch(IllegalStateException e){
				e.printStackTrace();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		int result = goodsInfoService.add(gf);
		if(result > 0) {
			return new ResultVO(200, "成功");
		}
		return new ResultVO(500, "失败");
	}
	/**
	 * 处理首页查询
	 * @return
	 */
	@GetMapping("/finds")
	public ResultVO finds() {
		Map<String, Object> map = goodsInfoService.findIndex();
		if(map == null || map.isEmpty()) {
			return new ResultVO(600, "暂无数据");
		}
		return new ResultVO(200, "成功", map);
	}
	/**
	 * 处理根据商品类型分页查询的第一次查询
	 * @param tno
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/findByFirst")
	public ResultVO findByFirst(String tno, Integer page, Integer rows) {
		Map<String, Object> map = goodsInfoService.findByFirsh(tno, page, rows);
		if(map == null || map.isEmpty()) {
			return new ResultVO(600, "暂无数据");
		}
		return new ResultVO(200,"成功",map);
	}
	/**
	 * 
	 * @param tno
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("/findByPage")
	public ResultVO findByTno(String tno, Integer page, Integer rows) {
		List<GoodsInfo> map = goodsInfoService.findByType(tno, page, rows);
		if(map == null || map.isEmpty()) {
			return new ResultVO(600, "暂无数据");
		}
		return new ResultVO(200,"成功",map);
	}
}
